#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   http_htaccess.sh
# Version:    1.0
# Date:       2013/11/12
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-08Apache-01字符模式-08htaccess与认证网页设置
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/12
#             - httpd: htaccess
# ----------------------------------------------------------------------

##! @TODO: setup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    trap do_cleanup INT EXIT
    serv=httpd
    apache_user=apache

    user_exists ${apache_user}
    EchoResult "check user ${apache_user}"

    server_ip=`ifconfig -a | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    if [ -z ${server_ip} ]; then
        echo "failed: get local ip null, pls check"
        exit 1
    fi

    
    # create dirs
    protect_dir=/var/www/html/protect
    [ -d ${protect_dir} ] && rm -rf ${protect_dir}
    mkdir ${protect_dir}
    EchoResult "mkdir ${protect_dir}"

    echo test > ${protect_dir}/index.html

    # http service
    # back httpd.conf
    HTTPCONF=/etc/httpd/conf/httpd.conf
    cp ${HTTPCONF} ${HTTPCONF}.bak
    EchoResult "back ${HTTPCONF}"

    # check .htaccess in httpd.conf
    #grep 'AccessFileName .htaccess' ${HTTPCONF}
    #if [ $? -ne 0 ]
    #then
    #    echo "${HTTPCONF} not include 'AccessFileName .htaccess'"
    #    exit 1
    #fi

    # add htaccess to httpd.conf
    cat >> ${HTTPCONF}  <<-EOF
<Directory "/var/www/html/protect">
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
</Directory>
EOF

    # restart
    restart_daemon ${serv}

    sleep 3
    # check httpd restart
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} restart"


    # .htaccess
    touch ${protect_dir}/.htaccess
    apache_passwd=/var/www/apache.passwd
    cat >> ${protect_dir}/.htaccess <<-EOF
AuthName "Protect test by .htaccess"
Authtype Basic
AuthUserFile ${apache_passwd}
require user ${apache_user}
EOF

    # set password for user, use htpasswd
    expect <<-END
        spawn htpasswd -c ${apache_passwd} ${apache_user}
            expect "New password:"
            send "qwer1234\r"
            expect "Re-type new password:"
            send "qwer1234\r"
            expect "Adding password for user"
            send_user "htpasswd success"
         expect eof
    
END



}

##! @TODO: test
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    rm -f /tmp/index.html*
    cd /tmp
    # get 
    wget --user=${apache_user} --password=qwer1234 http://${server_ip}/protect/index.html

    cd -

    # check 
    [ -f /tmp/index.html ] && diff /tmp/index.html ${protect_dir}/index.html
    EchoResult "wget http://${server_ip}/protect"
    
}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_cleanup()
{
    rm -rf ${protect_dir} ${apache_passwd}
    mv -f ${HTTPCONF}.bak ${HTTPCONF}
    stop_daemon ${serv}

    rm -f /tmp/index.html
}

# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${SFROOT}/lib/XmlParse.sh
. ${CMDLIB}/lib/Net.sh


DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue DHCPSTATICIP DhcpStaticIp

# main 
do_setup
do_test
EchoResult "testcase : ${serv} htaccess"
